package xcmg.device.web.company;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xcmg.device.infra.AuthCode;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.infra.enums.CompanyTypeEnum;
import xcmg.device.service.company.CompanyApplyService;
import xcmg.device.service.company.CompanyInfoService;
import xcmg.device.service.task.SyncMmPartSupplierTaskService;
import xcmg.device.service.vo.company.*;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;
import yb.ecp.fast.infra.util.Ref;
import yb.ecp.fast.infra.util.StringUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by sqq on 2017/12/26.
 */

@RestController
@RequestMapping("/company")
public class CompanyInfoController extends BasicController {

    @Autowired
    private CompanyInfoService companyInfoService;

    @Autowired
    private CompanyApplyService companyApplyService;

    @Autowired
    private SyncMmPartSupplierTaskService syncMmPartSupplierTaskService;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiOperation("获取企业列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult<PageInfo<CompanyShowVO>> list(@RequestBody SearchCommonVO<CompanyInfoVO> condition) throws Exception {
        PageCommonVO<CompanyShowVO> pageCommonVO = companyInfoService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation("企业信息增加")
    @FastMappingInfo(needLogin = true, code = AuthCode.Company.COMPANY_ADD)
    public ActionResult<Object> add(@RequestBody CompanyInfoVO companyInfoVO) throws Exception {
        companyInfoVO.setChannel(CompanyTypeEnum.Channel.PlatformInsert.getCode());
        companyInfoVO.setOrgId(getOrgId());
        companyInfoVO.setCreateDate(new Date());
        Ref<String> orgIdRef = new Ref<>("");
        ErrorCode errorCode = companyInfoService.insert(companyInfoVO, orgIdRef);
        return actionResult(errorCode, orgIdRef.get());
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation("企业信息更新")
    @FastMappingInfo(needLogin = true, code = AuthCode.Company.COMPANY_EDIT)
    public ActionResult<String> update(@RequestBody CompanyInfoVO companyInfoVO) throws Exception {
        if (companyInfoVO.getOrgId() == null) {
            return actionResult(ErrorCode.IllegalArument);
        }
        ErrorCode code = companyInfoService.update(companyInfoVO);
        return actionResult(code);
    }

    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public ActionResult<CompanyInfoVO> info(String orgId) {
        if (StringUtil.isNullOrEmpty(orgId)) {
            return null;
        }
        CompanyInfoVO companyInfoVO = companyInfoService.info(orgId);
        return actionValueResult(companyInfoVO);
    }

    @RequestMapping(value = "/syncMmPartSupplier", method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult<Object> syncMmPartSupplier(String orgId) {
        if (StringUtil.isNullOrEmpty(orgId)) {
            return null;
        }
        CompanyInfoVO companyInfoVO = companyInfoService.info(orgId);
        if (companyInfoVO == null) {
            return null;
        }
        syncMmPartSupplierTaskService.syncGpsPartSupplier(null, companyInfoVO.getSupplierNo(), null);
        return actionValueResult(companyInfoVO);
    }

    @RequestMapping(value = "/inviteCompanyInfo",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult inviteCompanyInfo(String invitationCode){
        CompanyInvitationInfoVO companyInvitationInfoVO =companyInfoService.inviteCompanyInfo(invitationCode);
        return actionValueResult(companyInvitationInfoVO);
    }

    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public ActionResult register(@RequestBody CompanyRegisterVO companyRegisterVO) throws Exception
    {
        Ref<String> orgIdRef = new Ref<>("");
        ErrorCode errorCode =  companyInfoService.register(companyRegisterVO);
        return actionResult(errorCode,orgIdRef.get());
    }




    @RequestMapping(value = "/infos", method = RequestMethod.POST)
    public ActionResult<List<CompanyInfoVO>> infos(@RequestBody ArrayList<String> orgIds) {
        if (CollectionUtils.isEmpty(orgIds)) {
            return null;
        }
        List<CompanyInfoVO> companyInfoVOs = companyInfoService.infos(orgIds);
        return actionValueResult(companyInfoVOs);
    }

    @RequestMapping(value = "/selfCompanyInfo", method = RequestMethod.GET)
    @ApiOperation(value = "查询当前用户所属企业信息")
    @FastMappingInfo(needLogin = true)
    public ActionResult<CompanyInfoVO> querySelfCompanyInfo(@RequestHeader("x-user-id") String userId) throws Exception {
        CompanyInfoVO companyInfoVO = companyInfoService.querySelfCompanyInfo(userId);
        return actionValueResult(companyInfoVO);
    }

    @RequestMapping(value = "/selfCompanyInfoById", method = RequestMethod.GET)
    @ApiOperation(value = "查询当前用户所属企业信息")
    @FastMappingInfo(needLogin = true)
    public ActionResult<CompanyInfoVO> selfCompanyInfoById(String userId) throws Exception {
        CompanyInfoVO companyInfoVO = companyInfoService.querySelfCompanyInfo(userId);
        return actionValueResult(companyInfoVO);
    }


    
    @RequestMapping(value = "/listUnBind",method = RequestMethod.POST)
    @ApiOperation("获取企业列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult listUnBind(@RequestBody SearchCommonVO<CompanySearchConditionVO> condition) throws Exception
    {
        PageCommonVO pageCommonVO = companyInfoService.listUnBind(condition, getOrgId());
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/applyRecordList",method = RequestMethod.POST)
    @ApiOperation("获取企业待审核列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult applyRecordList(@RequestBody SearchCommonVO<CompanyApplyVO> condition) throws Exception
    {
        PageCommonVO pageCommonVO = companyApplyService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/audit",method = RequestMethod.POST)
    @ApiOperation("企业注册信息审核")
    @FastMappingInfo(needLogin = true,code = AuthCode.Company.AUDIT_COMPANY)
    public ActionResult audit(@RequestBody AuditApplyVO auditApplyVO,@RequestHeader("x-user-id") String userId) throws Exception
    {
        auditApplyVO.setAuditName(getUserName());
        auditApplyVO.setAuditId(userId);
        return actionResult(companyApplyService.audit(auditApplyVO));
    }

    @RequestMapping(value = "/bound",method = RequestMethod.POST)
    @ApiOperation("绑定上游企业")
    @FastMappingInfo(needLogin = true)
    public ActionResult boundDealer(@RequestBody CompanyApplyVO companyApplyVO) throws Exception
    {
        return actionResult(companyApplyService.boundDealer(companyApplyVO,getOrgId()));
    }

    @RequestMapping(value = "/boundSupplier",method = RequestMethod.POST)
    @ApiOperation("绑定下游企业")
    @FastMappingInfo(needLogin = true)
    public ActionResult boundSupplier(@RequestBody CompanyApplyVO companyApplyVO) throws Exception
    {
        return actionResult(companyApplyService.boundSupplier(companyApplyVO,getOrgId()));
    }



    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ApiOperation("企业信息删除")
    @FastMappingInfo(needLogin = true,code = AuthCode.Company.COMPANY_DEL)
    public ActionResult delete(@RequestBody List<String> orgIds){
        Ref<List<String>> faildRef = new Ref<>(null);
        return actionResult(companyInfoService.delete(orgIds,faildRef),faildRef.get());
    }



    @RequestMapping(value = "/export",method =RequestMethod.POST)
    @ApiOperation("导出所选企业信息")
    @FastMappingInfo(needLogin = true,code = AuthCode.Company.COMPANY_EXPORT)
    public void export( String[] companyIds) throws Exception
    {
      companyInfoService.export(Arrays.asList(companyIds));
    }

    @RequestMapping(value = "/exportAll",method = RequestMethod.POST)
    @ApiOperation("导出全部企业信息")
    @FastMappingInfo(needLogin = true,code = AuthCode.Company.COMPANY_EXPORT)
    public void exportAll( CompanyInfoVO companyInfoVO) throws Exception
    {
        companyInfoService.exportAll(companyInfoVO);
    }
    @RequestMapping(value = "/updateInfo",method = RequestMethod.POST)
    @ApiOperation("企业信息更新")
    @FastMappingInfo(needLogin = true)
    public ActionResult updateInfo(@RequestBody CompanyInfoVO companyInfoVO) throws Exception{
        if(companyInfoVO.getOrgId() == null ){
           companyInfoVO.setOrgId(getOrgId());
        }
        ErrorCode code = companyInfoService.updateInfo(companyInfoVO);
        return actionResult(code);
    }

    @RequestMapping(value = "/itemByInvitation",method = RequestMethod.GET)
    @ApiOperation("查询邀请码发送方信息")
    @FastMappingInfo(needLogin = true)
    public ActionResult itemByInvitationCode(String invitationCode)
    {
        CompanyInfoVO companyInfoVO = companyInfoService.itemByInvitationCode(invitationCode);
        if (null == companyInfoVO)
        {
            return actionResult(ErrorCode.InvitationCodeFail);
        }
        return actionValueResult(companyInfoVO);
    }

//    @RequestMapping(value = "/getBySapCode",method = RequestMethod.GET)
//    @ApiOperation("根据sapCode查询公司信息")
//    @FastMappingInfo(needLogin = true)
//    public ActionResult getBySapCode(String sapCode) throws Exception{
//        return actionResult(companyInfoService.getBySapCode(sapCode));
//    }
//
//    @RequestMapping(value = "/listCoreComs",method = RequestMethod.POST)
//    @ApiOperation("查询所有主机厂")
//    @FastMappingInfo(needLogin = true)
//    public ActionResult listCoreComs(CompanyInfoVO companyInfoVO) throws Exception{
//        return actionResult(companyInfoService.listCoreComs(companyInfoVO));
//    }

//    @RequestMapping(value = "/listCoreComsForCore",method = RequestMethod.POST)
//    @ApiOperation("查询所有所有的核心企业")
//    @FastMappingInfo(needLogin = true)
//    public ActionResult listCoreComsForCore(CompanyInfoVO companyInfoVO) throws Exception{
//        return actionResult(companyInfoService.listCoreComsForCore(companyInfoVO));
//    }
////
//    @RequestMapping(value = "/isFirstLevelCompany",method = RequestMethod.GET)
//    @FastMappingInfo(needLogin = true)
//    public ActionResult isFirstLevelCompany(String orgId) throws Exception {
//        return actionResult(companyInfoService.isFirstLevelCompany(orgId));
//    }
}
